/*
package jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

*/
/**
 * 完成用户注册功能
 * 要求：
 * 程序启动后，提示用户顺序输入：用户名，密码，昵称，年龄
 * 然后将该用户信息插入到user表中并提示用户注册成功
 *//*

*/
/*
public class RegDemo {
    public static void main(String[] args) throws Exception
    {
*//*

*/
/*        try(Connection connection = DBUtil.getConnection();){
            System.out.println("与数据库正常链接！");
            Statement statement = connection.createStatement();
            String sql = "INSERT INTO user(name,password,nickname,age) " +
                    "VALUES('zs','123456','az',22)";
            int num = statement.executeUpdate(sql);
            if(num>0){
                System.out.println("注册成功");
            }else {
                System.out.println("注册失败");
            }
        }catch (SQLException e){
            e.printStackTrace();
        }*//*

*/
/*

        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎注册");
        System.out.println("请输入用户名");
        String name = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        System.out.println("请输入昵称");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄");
        int age = Integer.parseInt(scanner.nextLine());
        System.out.println(name + "," + password + "," + nickname + "," + age);
        try(Connection connection = DBUtil.getConnection()){
            Statement statement = connection.createStatement();
            String sql = "INSERT INTO user(name,password,nickname,age) " +
                    "VALUES('"+ name +"','"+"','"+ password +"','"+ nickname + "'," + age +")";
            System.out.println(sql);
            int num = statement.executeUpdate(sql);
            System.out.println("注册成功");
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
*//*

public class RegDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎注册");
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        System.out.println("请输入昵称");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄");
        int age = Integer.parseInt(scanner.nextLine());
        System.out.println(username + "," + password + "," + nickname + "," + age);

        try (
                Connection connection = DBUtil.getConnection();
        ) {
            Statement statement = connection.createStatement();
            String sql = "INSERT INTO user(username,password,nickname,age) " +
                    "VALUES('" + username + "','" + password + "','" + nickname + "'," + age + ")";
            System.out.println(sql);
            int num = statement.executeUpdate(sql);
            System.out.println("注册成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}*/
package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * 完成用户注册功能
 * 要求：
 * 程序启动后，提示用户顺序输入：用户名，密码，昵称，年龄
 * 然后将该用户信息插入到user表中并提示用户注册成功
 */
public class RegDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎注册");
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        System.out.println("请输入昵称");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄");
        int age = Integer.parseInt(scanner.nextLine());
        System.out.println(username + "," + password + "," + nickname + "," + age);

        try (Connection connection = DBUtil.getConnection()) {
            // 使用 PreparedStatement 构建 SQL 语句
            String sql = "INSERT INTO user(name, password, nickname, age) VALUES (?, ?, ?, ?)";
            try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
                pstmt.setString(1, username);
                pstmt.setString(2, password);
                pstmt.setString(3, nickname);
                pstmt.setInt(4, age);

                int num = pstmt.executeUpdate();
                if (num > 0) {
                    System.out.println("注册成功");
                } else {
                    System.out.println("注册失败");
                }
            }
        } catch (Exception e ) {
            e.printStackTrace();
        }
    }
}
